import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router';
import { ShopService } from 'src/app/services/shop.service';

import * as constants from '../../../providers/Constants'
import { ToastService } from 'src/app/providers/toast/toast.service';
import { NavController } from '@ionic/angular';
import { Utils } from 'src/app/providers/Utils';

@Component({
  selector: 'app-order-comment',
  templateUrl: './order-comment.page.html',
  styleUrls: ['./order-comment.page.scss'],
})
export class OrderCommentPage implements OnInit {
  commentInfo: any = {};
  content: string = '';
  classInfo: number = 0;//分为10个等级 0-9
  classInfoText: string = '';//分为10个等级 0-9
  constructor(private activeRoute: ActivatedRoute, private nav: NavController, private shopService: ShopService, private toast: ToastService) {
    this.activeRoute.queryParams.subscribe((params: Params) => {
      if (params.commentInfo) {
        this.commentInfo = JSON.parse(params.commentInfo);
        this.commentInfo.array.forEach(element => {
          element.classInfo = 0;
          element.classInfoText = '';
          element.content = '';
        });
      }
    })

  }

  ngOnInit() {
    console.log(this.commentInfo);

  }
  btnSubmit() {
    if (this.commentInfo.procOrderStatus != '0') {
      this.toast.show('Error');
      return;
    }
    console.log('------------', this.commentInfo.array);
    let array = [];
    let classInfoText = null; //星星评价
    let content = null; //发表意见
    for (let index = 0; index < this.commentInfo.array.length; index++) {
      const element = this.commentInfo.array[index];
      if (!element.classInfoText) {
        if (classInfoText == null) {
          classInfoText = index;
        }
      } else if (!element.content) {
        if (content == null) {
          content = index;
        }
      }
      array.push({
        procOrderId: element.procOrderId,
        procId: element.procId,
        comment: element.content,
        appraise: element.classInfo
      });
    }
    if (classInfoText != null) {
      if (this.commentInfo.array.length != 1) {
        this.toast.show('Please select a' + (classInfoText + 1) );
      } else {
        this.toast.show('Please select');
      }
    } else if (content != null) {
      if (this.commentInfo.array.length != 1) {
        this.toast.show('Please input' + (content + 1) );
      } else {
        this.toast.show('Please input');
      }
    } else {
      this.shopService.writeComment({
        array: array
      }).subscribe(res => {
        // 保存成功返沪上一页，否则弹出提示框
        if (res.retCode == '000000') {
          this.nav.pop();
        } else if (constants.errCode[res.retCode]) {
          if (constants.errCode[res.retCode] != '0') {
            this.toast.show(constants.errCode[res.retCode]);
          }
        } else if (res.retMsg) {
          this.toast.show(res.retMsg);
        } else {
          this.toast.show("exception service routine");
        }
      });

    }
    console.log(this.commentInfo.array);

  }
  onClickStar(index, idx) {
    this.classInfo = index + 3;
    if (index == 0) {
      this.classInfoText = '非常差'
    } else if (index == 2) {
      this.classInfoText = '差'
    } else if (index == 4) {
      this.classInfoText = '一般'
    } else if (index == 6) {
      this.classInfoText = '好'
    } else if (index == 8) {
      this.classInfoText = '非常好'
    }
    this.classInfoText = ''
    this.commentInfo.array[idx].classInfoText = this.classInfoText;
    this.commentInfo.array[idx].classInfo = this.classInfo;
  }
  AmountOfConversion(num: number) {
    return num / 100;
  }
  solaImage(img){
    return Utils.imageUrl(this.shopService.getImageUrl(),img);
  }

}
